package com.ybkj.chat.module.login.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.bigkoo.pickerview.view.TimePickerView;
import com.ybkj.chat.R;
import com.ybkj.chat.base.BaseMvpActivity;
import com.ybkj.chat.module.login.presenter.Register2Presenter;
import com.ybkj.chat.module.login.view.Register2View;
import com.ybkj.chat.ui.dialog.PictureSelectDialog;
import com.ybkj.chat.ui.dialog.TipDialog;
import com.ybkj.chat.ui.view.TimeSelectView;
import com.ybkj.chat.utils.DateUtil;
import com.ybkj.chat.utils.EditTextInputHelper;
import com.ybkj.chat.utils.ToastUtil;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册第二步
 */
public class Register2Activity extends BaseMvpActivity<Register2Presenter> implements Register2View {
    //昵称
    @BindView(R.id.et_name)
    EditText etName;
    //输入密码
    @BindView(R.id.et_password)
    EditText etPassword;
    //出生日期
    @BindView(R.id.et_born)
    EditText etBorn;
    //男
    @BindView(R.id.rb_male)
    RadioButton rbMale;
    //女
    @BindView(R.id.rb_female)
    RadioButton rbFemale;
    @BindView(R.id.register_finish)
    Button registerFinish;

    private EditTextInputHelper mEditTextInputHelper;
    private TimePickerView timeSelectView;
    //图片选择弹框
    private PictureSelectDialog pictureSelectDialog;

    private TipDialog dialog;//性别确定dialog

    @Override
    protected void injectPresenter() {

    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register2;
    }

    @Override
    protected void initView() {
        timeSelectView = TimeSelectView.getTimePicker(mContext, new TimeSelectView.TimerListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                etBorn.setText(DateUtil.dateToString(date, DateUtil.dateFormat4));
            }

            @Override
            public void onCancel() {
                timeSelectView.dismiss();
            }

            @Override
            public void onConform() {
                timeSelectView.returnData();
                timeSelectView.dismiss();
            }
        }, 1);

        mEditTextInputHelper = new EditTextInputHelper(registerFinish, false);
        mEditTextInputHelper.addViews(etName, etPassword);

    }

    @Override
    protected void initData() {

    }

    @Override
    public boolean isImmersiveStatusBar() {
        return true;
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.et_born, R.id.register_finish, R.id.ic_head})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //选择出生日期
            case R.id.et_born:
                timeSelectView.show();
                break;
            //注册完成
            case R.id.register_finish:
                if (dialog != null) {
                    dialog.show();
                } else {
                    dialog = new TipDialog(mContext);
                    dialog.setContentText("选择确认后，性别不可更改");
                    dialog.setOnConfirmButtonClickListener((dialog, view1) -> {
                        dialog.dismiss();
                    });
                }
                break;
            //头像选择
            case R.id.ic_head:
                if (pictureSelectDialog == null)
                    initPictureSelectDialog();
                pictureSelectDialog.show();
                break;
        }
    }

    private void initPictureSelectDialog() {
        pictureSelectDialog = new PictureSelectDialog(mContext);
        pictureSelectDialog.setCrop(false);
        pictureSelectDialog.setOutputSize(1000, 1000);
        pictureSelectDialog.setOnSelectSuccessListener(new PictureSelectDialog.OnSelectSuccessListener() {
            @Override
            public void onBytesSuccess(byte[] bytes, int tag) {
                ToastUtil.showShort("bytes.length=" + bytes.length);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mEditTextInputHelper.removeViews();
        if (dialog != null) {
            dialog.cancel();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (pictureSelectDialog != null)
            pictureSelectDialog.onActivityResult(requestCode, resultCode, data);
    }


}


